home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / contrib / russo / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-25  |  4.9 KB  |  256 lines

  1. /* util.c - Utilities for GNUPLOTIO */
  2.  
  3. #include <stdio.h>    /* FILENAME_MAX, NULL */
  4. #include <stdlib.h>    /* getenv */
  5. #include <string.h>    /* strcpy/cat */
  6. #include <sys/stat.h>    /* stat */
  7. #include <sys/time.h>    /* select */
  8. int bzero(void *, int); /* this isn't in a header file !? */
  9. #include "util.h"
  10.  
  11. /*
  12. || check_fd_for_reading
  13. ||
  14. || Find out whether a read(fd,...) would block.
  15. ||
  16. */
  17. int check_fd_for_reading(int fd)
  18. {
  19. int nfound, nfds;
  20. fd_set readfds;
  21. static struct timeval timeout = {0, 100};
  22.  
  23.     nfds = fd+1;
  24.     FD_ZERO(&readfds);
  25.     FD_SET(fd, &readfds);
  26.     nfound = select(nfds,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout);
  27.     if (nfound > 0 && FD_ISSET(fd, &readfds))
  28.         return 1;
  29.     return 0;
  30. }
  31.  
  32.  
  33. /*
  34. || count_items
  35. ||
  36. || Return number of columns in the input buf, where a column is
  37. || defined to be a contiguous group of non-whitespace characters.
  38. ||
  39. */
  40. #define NONWHITE 1
  41. #define ISWHITE 0
  42.  
  43. int count_items(const char buf[])
  44. {
  45. char ch;
  46. int lastchar,i,items;
  47.  
  48.     /* blow off leading white-space */
  49.     i=0;
  50.     while(buf[i] == ' ' || buf[i] == '\t' || buf[i] == '\n') i++;
  51.  
  52.     if (buf[i] == '\0')
  53.         return 0;
  54.  
  55.     items = 0;
  56.     lastchar = NONWHITE;
  57.     do
  58.     {
  59.         ch = buf[i];
  60.         if(buf[i]== ' '||buf[i]== '\t'||buf[i]== '\n' ||buf[i] == '\0')
  61.         {    /* char is white-space */
  62.             if(lastchar == NONWHITE)
  63.                 items++;
  64.             lastchar = ISWHITE;
  65.         }
  66.         else 
  67.         {    /* current char isn't a white space */
  68.             lastchar = NONWHITE;
  69.         }
  70.         i++;
  71.     } while(ch != '\n' && ch != '\0');
  72.     return items;
  73. }
  74.  
  75. #undef ISWHITE
  76. #undef NONWHITE
  77.  
  78. /*
  79. || get_dir()
  80. ||
  81. || return directory strings extracted from a colon-separated
  82. || $PATH-style string. Mal-formed paths (eg "/bin::/usr/bin:")
  83. || pose no problem and are handled as expected.
  84. ||  
  85. || Example use:
  86. ||
  87. ||  int i=0;        (init i=0 and don't change it)
  88. ||  char *dir;
  89. ||  char *pathvar = "/bin:/usr/bin:/usr/etc:/usr/local/bin";
  90. ||  :
  91. ||  while ((dir=get_dir(pathvar, &i)) != NULL)
  92. ||  { 
  93. ||    :    (use dir in this iteration)
  94. ||  }
  95. ||
  96. */
  97. char *get_dir(const char *sp, int *pi)
  98. {
  99. static char dir[FILENAME_MAX];
  100. int i,j;
  101.  
  102.     /* *pi is the first char to test in this invocation */
  103.  
  104.     if (sp[*pi] == '\0')
  105.     {
  106.         *pi = 0;
  107.         return NULL;
  108.     }
  109.  
  110.     for (i = *pi, j=0 ; ; i++)
  111.     {
  112.         switch (sp[i])
  113.         {
  114.         case ':':
  115.         dir[j] = '\0';
  116.         if (j != 0)    /* "::" case, we cycle */
  117.         {
  118.             *pi = i+1;    /* get on other side of : for next invocation */
  119.             return dir;
  120.         }
  121.         break; 
  122.         case '\0':
  123.         dir[j] = '\0';
  124.         if (j != 0)
  125.         {
  126.             *pi = i;
  127.             return dir;
  128.         }
  129.         else        /* ":\0" case */
  130.         {
  131.             *pi = 0;
  132.             return NULL;
  133.         }
  134.         /* NOTREACHED */
  135.         break;
  136.         default:
  137.         dir[j] = sp[i];    /* accumulate result string */
  138.         j++;
  139.         break;
  140.         }
  141.     }
  142. }
  143.  
  144.  
  145. /*
  146. || can_run
  147. ||
  148. || Look for the program named in the prog input in the user's $PATH...
  149. || Return OK        if an exec(prog) would work.
  150. ||        NOT_FOUND if prog is not in the $PATH or $PATH is not set.
  151. ||        NOT_EXEC  if prog is found but isn't executable for the user
  152. ||                  or isn't a regular file.
  153. */
  154. int can_run(const char *prog)
  155. {
  156. char *searchpath;
  157. int p, status;
  158. char *dir;
  159. char abspath[FILENAME_MAX];
  160. struct stat statbuf;
  161.  
  162.     if ((searchpath = getenv("PATH")) == NULL)
  163.         return NOT_FOUND;
  164.     
  165.     p = 0;
  166.     status = NOT_FOUND;    /* assume not found */
  167.     while ((dir = get_dir(searchpath, &p)) != NULL)
  168.     {
  169.         strcpy(abspath, dir);
  170.         strcat(abspath, "/");
  171.         strcat(abspath, prog);
  172.         if (stat(abspath, &statbuf) != -1)
  173.         {
  174.         if (S_ISREG(statbuf.st_mode))
  175.         {
  176.             if (statbuf.st_mode & S_IXUSR ||statbuf.st_mode & S_IXGRP ||
  177.                statbuf.st_mode & S_IXOTH)
  178.             status = OK;    /* ok */
  179.             else
  180.             status = NOT_EXEC;    /* not executable */
  181.         }
  182.         else /* not a regular file */
  183.             status = NOT_EXEC;
  184.         break;
  185.         }
  186.     }
  187.  
  188.     return status;
  189. }
  190.  
  191.  
  192. /*
  193. || print_nonprint
  194. ||
  195. || Print a string, map its non-printing characters into a visible
  196. || representation:
  197. ||    HOW_OCTAL  \nnn
  198. ||    HOW_HEX    \xhh
  199. ||    HOW_ESCAPE \a \b \f \n \r \t \v \nnn
  200. ||    HOW_QUES   ?
  201. */
  202. void print_nonprint(const char *buf, int how)
  203. {
  204.     while (*buf != '\0')
  205.     {
  206.         if (isprint((int) *buf))
  207.         fputc((int)(*buf),stdout);
  208.         else
  209.         switch (how)
  210.         {
  211.         case HOW_OCTAL:
  212.             printf("\\%03o",(unsigned) *buf);
  213.             break;
  214.         case HOW_HEX:
  215.             printf("\\x%02x",(unsigned) *buf);
  216.             break;
  217.         case HOW_ESCAPE:
  218.             switch (*buf)
  219.             {
  220.             case '\a':            /* alert (bell) */
  221.             fputs("\\a",stdout);
  222.             break;
  223.             case '\b':            /* backspace */
  224.             fputs("\\b",stdout);
  225.             break;
  226.             case '\f':            /* formfeed */
  227.             fputs("\\f",stdout);
  228.             break;
  229.             case '\n':            /* newline */
  230.             fputs("\\n",stdout);
  231.             break;
  232.             case '\r':            /* carriage return */
  233.             fputs("\\r",stdout);
  234.             break;
  235.             case '\t':            /* horiz tab */
  236.             fputs("\\t",stdout);
  237.             break;
  238.             case '\v':            /* vertical tab */
  239.             fputs("\\v",stdout);
  240.             break;
  241.             default:
  242.                 printf("\\%03o",(unsigned) *buf);
  243.             /*fputc('?',stdout);*/
  244.             break;
  245.             }
  246.             break;
  247.         case HOW_QUES:
  248.         default:
  249.             fputc('?',stdout);
  250.             break;
  251.         }
  252.         buf++;
  253.     }
  254.     printf("\n");
  255. }
  256.